package org.vestbjergbyggecenter.TUI;

import java.util.Scanner;

import org.vestbjergbyggecenter.Controller.ProductCtr;

public class ProductMenu {
		
		    private ProductCtr productCtr;
		    private static ProductMenu instance = null;
		    
		    private ProductMenu()
		    {
		        productCtr = ProductCtr.getInstance();
		        ProductMenuStart();
		    }
		    
		    public static ProductMenu getInstance()
		    {
		        if(instance == null)
		        {
		            instance = new ProductMenu();
		        }
		        return instance;
		    }
		    
		   //  start menu
		   public void ProductMenuStart()
		    {
		        boolean exit = false;
		        while(!exit)
		        {
		            int choice = printProductMenu();
		            switch (choice)
		            {
		                case 1: listAll();
		                        break;
		                case 2: registerProduct();
		                        break;
		                case 3: readProduct();
		                        break;
		                case 4: updateProduct();
		                        break;
		                case 5: deleteProduct();
		                        break;
		                case 6: goBack();
		                        break;
		            }
		        }
		    
		    }
		   
		   // initialization of the product menu
			public int printProductMenu()
		    {
		        Scanner keyboard = new Scanner(System.in);
		        System.out.print('\f');
		        System.out.println("***Product Menu***");
		        System.out.println("1. List all products");
		        System.out.println("2. Register new product");
		        System.out.println("3. Read product data");
		        System.out.println("4. Update product data");
		        System.out.println("5. Delete product");
		        System.out.println("6. Go back");
		        System.out.println("Choice #: ");
		        
		        int choice = keyboard.nextInt();
		        return choice;
		    }			 
			
			public void listAll()
			{
				productCtr.printAllProducts();				
			}
			
			
		    // chooses of the register window
			public void registerProduct()
		    {
					Scanner keyboard = new Scanner(System.in); 
			    	System.out.print('\f');
			    	System.out.println("Input product id: ");
			    	int id = Integer.parseInt(keyboard.nextLine());
			        System.out.println("Input the name of the product: ");
			        String name = keyboard.nextLine();
			        System.out.println("Input the category of the product : ");
			        String category = keyboard.nextLine();
			        System.out.println("Choose the price of the product: ");
			        double price = Double.parseDouble(keyboard.nextLine());
			        System.out.println("Input the quantity of the product: ");
			        int quantity = Integer.parseInt(keyboard.nextLine());
			        System.out.println("Choose the location for the product: ");
			        String location = keyboard.nextLine();
			        if(productCtr.getProduct(id) == null)
			        {
				        productCtr.createProduct(id ,name, category, price, quantity, location); 
			        }
			        else
			        {
			            System.out.println("Error: Id already exists in the Product Collection");
			        }			    
		    }
			
			public void readProduct()
		    {
				Scanner keyboard = new Scanner(System.in); 
		        System.out.print('\f');
		        System.out.println("Input product id: ");
		        int id = Integer.parseInt(keyboard.nextLine());
		        
		        if(productCtr.getProduct(id) != null)
		        {
			        productCtr.printProduct(id);
		        }
		        else
		        {
		            System.out.println("Error: Product not found");
		        }			    
		    }
			
		    // chooses of the update window
			public void updateProduct()
		    {
				 Scanner keyboard = new Scanner(System.in); 
			        System.out.print('\f');
			        System.out.println("Input new product id: ");
			        int id = Integer.parseInt(keyboard.nextLine());
			        System.out.println("Input the new name of the product: ");
			        String name = keyboard.nextLine();
			        System.out.println("Input the new category of the product : ");
			        String category = keyboard.nextLine();
			        System.out.println("Choose the new price of the product: ");
			        int price = keyboard.nextInt();
			        System.out.println("Input the new quantity of the product: ");
			        int quantity = keyboard.nextInt();
			        System.out.println("Choose the new location for the product: ");
			        String location = keyboard.nextLine();
			        
			        productCtr.updateProduct(id, name, category, price, quantity, location); 
		    }
			
		    // chooses of the delete window
		    public void deleteProduct()
		    {
		        Scanner keyboard = new Scanner(System.in);
		        System.out.print('\f');
		        System.out.println("Remove product: ");
		        int id = keyboard.nextInt();
		        if(productCtr.getProduct(id) != null)
		        {
			        productCtr.deleteProduct(id);
		            System.out.println("Product deleted.");
		        }
		        else
		        {
		            System.out.println("Error: Product not found");
		        }
		        printReturn();
		    }		    
		    		 
		    public void printReturn()
		    {
		        Scanner keyboard = new Scanner(System.in);
		        System.out.print("\nPress Enter to return...");
		        keyboard.nextLine();
		        printProductMenu();
		    }
		    
		    // calls and returns to main menu
		    public void goBack()
		    {
		        MainMenu.getInstance();
		    }
		}


